package com.foreveredu.propms.web.struts.action;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.foreveredu.propms.domain.News;
import com.foreveredu.propms.domain.PageModel;
import com.foreveredu.propms.service.IManagementService;
import com.foreveredu.propms.web.struts.actionform.NewsActionForm;

public class NewsAdminAction extends AbstractBaseDispatchAction {

	private IManagementService managementService = null;

	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}

	public ActionForward toAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("toAdd");
	}

	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		NewsActionForm newsform = (NewsActionForm) form;
		News news = new News();
		newsform.setNewscontent(newsform.getFckContent());
		PropertyUtils.copyProperties(news, newsform);
		// 添加新闻的表单校验
		if (newsform.getNewstopic() == null
				|| "".equals(newsform.getNewstopic())) {
			request.setAttribute("news", news);
			request.setAttribute("newsTopicError", "请输入动态标题！");
			return mapping.findForward("toAdd");
		}
		if (newsform.getFckContent() == null
				|| "".equals(newsform.getFckContent())) {
			request.setAttribute("news", news);
			request.setAttribute("newsContentError", "请输入动态内容！");
			return mapping.findForward("toAdd");
		}
		if (newsform.getBelong() == 0) {
			request.setAttribute("news", news);
			request.setAttribute("newsBelongError", "请选择动态属性！");
			return mapping.findForward("toAdd");
		}

		SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd");
		news.setNewstime(time.format(new Date()));
		news.setUpdatetime(time.format(new Date()));
		managementService.saveNews(news);
		if (newsform.getBelong() == 1) {
			return mapping.findForward("newsAdminList");
		}
		if (newsform.getBelong() == 2) {
			return mapping.findForward("zcfgAdminList");
		}

		return null;
	}

	// 通过ID查找news详情
	public ActionForward findNewsById(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		NewsActionForm newsform = (NewsActionForm) form;
		News news = managementService.findNewsById(newsform.getId());
		request.setAttribute("news", news);
		news.setTotal(news.getTotal() + 1);
		managementService.updateNews(news);
		return mapping.findForward("findNewsById");
	}

	public ActionForward findNewsByBelong(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		NewsActionForm newsform = (NewsActionForm) form;
		News news = managementService.findNewsByBelong(newsform.getBelong());
		request.setAttribute("news", news);
		managementService.updateNews(news);
		return mapping.findForward("toUpdateNews");
	}

	// News 翻页列表
	public ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		NewsActionForm newsform = (NewsActionForm) form;
		int offset = 0;
		String pageOffset = request.getParameter("pager.offset");// 获得翻页参数，即下一开始的纪录位置
		if (pageOffset != null && !pageOffset.equals("")) {
			offset = Integer.parseInt(pageOffset);
		}
		if (offset < 0) {
			return mapping.findForward("newsList");
		}
		PageModel pm = managementService.findNewsList(offset,
				newsform.getBelong());
		request.setAttribute("pm", pm);
		if (newsform.getBelong() == 1) {
			return mapping.findForward("newsList");
		}
		if (newsform.getBelong() == 2) {
			return mapping.findForward("zcfgList");
		}
		return null;
	}

	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		NewsActionForm newsform = (NewsActionForm) form;
		managementService.deleteNewsById(newsform.getId());

		if (newsform.getBelong() == 1) {
			return mapping.findForward("newsAdminList");
		}
		if (newsform.getBelong() == 2) {
			return mapping.findForward("zcfgAdminList");
		}
		return mapping.findForward("delete");
	}

	public ActionForward toUpdateNews(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		NewsActionForm newsform = (NewsActionForm) form;
		News news = managementService.findNewsById(newsform.getId());
		request.setAttribute("news", news);
		return mapping.findForward("toUpdateNews");
	}

	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		NewsActionForm newsform = (NewsActionForm) form;
		News news = managementService.findNewsById(newsform.getId());
		if (newsform.getNewstopic() == null
				|| "".equals(newsform.getNewstopic())) {
			request.setAttribute("news", news);
			request.setAttribute("newsTopicError", "请输入动态标题！");
			return mapping.findForward("toUpdateNews");
		}
		if (newsform.getFckContent() == null
				|| "".equals(newsform.getFckContent())) {
			request.setAttribute("news", news);
			request.setAttribute("newsContentError", "请输入动态内容！");
			return mapping.findForward("toUpdateNews");
		}
		if (newsform.getBelong() == 0) {
			request.setAttribute("news", news);
			request.setAttribute("newsBelongError", "请选择动态属性！");
			return mapping.findForward("toUpdateNews");
		}
		if (newsform.getBelong() == 3) {
			if (managementService.findNewsById((newsform.getId())).getBelong() != 3) {
				request.setAttribute("news", news);
				request.setAttribute("newsBelongError",
						"公司简介已经拥有，且具有唯一性，请选择其他动态！");
				return mapping.findForward("toUpdateNews");
			}
		}
		if (managementService.findNewsById((newsform.getId())).getBelong() == 3) {
			if (newsform.getBelong() != 3) {
				request.setAttribute("news", news);
				request.setAttribute("newsBelongError",
						"公司简介不能修改为其他动态！");
				return mapping.findForward("toUpdateNews");
			}
		}
		
		if (newsform.getBelong() == 4) {
			if (managementService.findNewsById((newsform.getId())).getBelong() != 4) {
				request.setAttribute("news", news);
				request.setAttribute("newsBelongError",
						"人才招聘已经拥有，且具有唯一性，请选择其他动态！");
				return mapping.findForward("toUpdateNews");
			}
		}
		if (managementService.findNewsById((newsform.getId())).getBelong() == 4) {
			if (newsform.getBelong() != 4) {
				request.setAttribute("news", news);
				request.setAttribute("newsBelongError",
						"人才招聘不能修改为其他动态！");
				return mapping.findForward("toUpdateNews");
			}
		}
		news.setNewstopic(newsform.getNewstopic());
		news.setBelong(newsform.getBelong());
		news.setNewscontent(newsform.getFckContent());

		SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd");
		news.setUpdatetime(time.format(new Date()));

		managementService.updateNews(news);

		if (newsform.getBelong() == 1) {
			return mapping.findForward("newsAdminList");
		}
		if (newsform.getBelong() == 2) {
			return mapping.findForward("zcfgAdminList");
		}
		return null;
	}
}
